import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.Vector;

import javax.swing.*;

public class ReadWithScanner {


	/**
   Constructor.
   @author Matthew Rowe
   @param aFileName full name of an existing, readable file.
	 * @throws FileNotFoundException 
	 */
	public ReadWithScanner(File aFileName) throws FileNotFoundException{
		fFile = aFileName;  
		//processLineByLine();
	}
	
	
	/** Template method that calls {@link #processLine(String)}.  */
	@SuppressWarnings("finally")
	public final List<Vector<Double>> processLineByLine() throws FileNotFoundException {
		//Note that FileReader is used, not File, since File is not able to be closed
		Scanner scanner = new Scanner(new FileReader(fFile));
		List<Vector<Double>> Data = new LinkedList<Vector<Double>>();
		Data = new ArrayList<Vector<Double>>();
		Vector<Double> X = new Vector<Double>();
		Vector<Double> Y = new Vector<Double>();
		Vector<Double> Yaw = new Vector<Double>();
		Vector<Double> Right = new Vector<Double>();
		Vector<Double> Door = new Vector<Double>();
		int i =0;
		try {
			//first use a Scanner to get each line
			while ( scanner.hasNextLine() ){
				processLine( scanner.nextLine() ,X,Y,Yaw,Right ,Door,i);
				i++;
			}
		}
		finally {
			//ensure the underlying stream is always closed
			//this only has any effect if the item passed to the Scanner
			//constructor implements Closeable (which it does in this case).
			Data.add(0,X);
			Data.add(1,Y);
			Data.add(2,Yaw);
			Data.add(3,Right);
			Data.add(4,Door);
			scanner.close();
			return  Data;
			
		}
	}

	/** 
   Overridable method for processing lines in different ways.

   <P>This simple default implementation expects simple name-value pairs, separated by an 
   '=' sign. Examples of valid input : 
   <tt>height = 167cm</tt>
   <tt>mass =  65kg</tt>
   <tt>disposition =  "grumpy"</tt>
   <tt>this is the name = this is the value</tt>
	 */
	protected void processLine(String aLine,Vector<Double> X, Vector<Double> Y, Vector<Double> Yaw, Vector<Double> Right, Vector<Double> Door, int i){
		//use a second Scanner to parse the content of each line 
		Scanner scanner = new Scanner(aLine);
		scanner.useDelimiter(" ");
		//System.out.println("INSIDE PROCESS LINE");
		if ( scanner.hasNext() ){
			//System.out.println("IN HAS NEXT 1 " + i);
			String x = scanner.next();
			//System.out.println("IN HAS NEXT 2 " + i);
			String y = scanner.next();
			//System.out.println("IN HAS NEXT 3 " + i);
			String yaw = scanner.next();
			//System.out.println("IN HAS NEXT 4 " + i);
			String right = scanner.next();
			//System.out.println("IN HAS NEXT 5 " + i);
			String left = scanner.next();
			String door = scanner.next();
			//System.out.println("IN HAS NEXT 6 " + i);
			X.add(Double.valueOf(x));
			//System.out.println("IN HAS NEXT 7 " + i);
			Y.add(Double.valueOf(y));
			//System.out.println("IN HAS NEXT 8 " + i);
			Yaw.add(Double.valueOf(yaw));
			//System.out.println("IN HAS NEXT 9 " + i);
			Right.add(Double.valueOf(right));
			//System.out.println("IN HAS NEXT 10 " + i);
			//System.out.println(x + " " + y + " " + yaw + " " + right + " " + left + " ");
			Door.add(Double.valueOf(door));
			}
		else {
			System.out.println("Empty or invalid line. Unable to process.");
		}
		//no need to call scanner.close(), since the source is a String
	}

	// PRIVATE 
	private final File fFile;

	
} 
